package com.loong.musicdemo.ui.playsong;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.loong.musicdemo.R;
import com.loong.musicdemo.adapter.PageSongListAdapter;
import com.loong.musicdemo.model.LocalPlay;
import com.loong.musicdemo.model.Song;
import com.loong.musicdemo.util.deleteutil.DeleteMusicUtil;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

/**
 * 下载在本地的歌曲列表,播放时直接从本地读取
 */
public class PageLocalFragment extends Fragment implements View.OnClickListener {
    private List<Song> localList = new ArrayList<>();//本地歌曲列表
    private PageSongListAdapter adapter;

    public PageLocalFragment() {
        //无参构造器
    }

    public static PageLocalFragment newInstance() {
        return new PageLocalFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取数据库信息
        initLocal();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_page_local, container, false);
        root.findViewById(R.id.play_current).setOnClickListener(this);
        root.findViewById(R.id.delete_current).setOnClickListener(this);
        RecyclerView recyclerLocalList = root.findViewById(R.id.current_list_recycler);
        adapter = new PageSongListAdapter(localList);
        //设置点击事件等
        adapter.setOnClickSongItem(new PageSongListAdapter.OnClickSongItem() {
            @Override
            public void onDeleteClick(int position) {
                //删除子项
                deleteDataById(localList.get(position).getSongId());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onSongClick(int position) {
                //播放歌曲

            }
        });
        recyclerLocalList.setAdapter(adapter);
        return root;
    }

    /**
     * 删除id的数据库历史记录
     *
     * @param songId 歌曲id
     */
    private void deleteDataById(String songId) {
        boolean b = DeleteMusicUtil.deleteMusicById(songId);
        if (b) {
            Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
            localList.clear();//清空信息
            List<LocalPlay> localPlays = LitePal.findAll(LocalPlay.class);//本地音乐播放信息
            for (LocalPlay localPlay : localPlays) {
                Song song = new Song();
                song.setSongId(localPlay.getSongId());
                song.setSingerName(localPlay.getAuthorName());
                song.setName(localPlay.getSongName());
                localList.add(song);
            }
            adapter.notifyDataSetChanged();
        } else {
            Toast.makeText(getContext(), "删除失败", Toast.LENGTH_SHORT).show();
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play_current:
            case R.id.delete_current:
                localList.clear();
                adapter.notifyDataSetChanged();
                boolean b = DeleteMusicUtil.deleteAllMusics();//删除所有的音乐
                if (b) {
                    Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                    localList.clear();//清空信息
                    List<LocalPlay> localPlays = LitePal.findAll(LocalPlay.class);//本地音乐播放信息
                    for (LocalPlay localPlay : localPlays) {
                        Song song = new Song();
                        song.setSongId(localPlay.getSongId());
                        song.setSingerName(localPlay.getAuthorName());
                        song.setName(localPlay.getSongName());
                        localList.add(song);
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    Toast.makeText(getContext(), "删除失败", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }


    /**
     * 从数据库中获取歌曲信息,
     * 再从内存中获取本地歌曲
     */
    private void initLocal() {
        List<LocalPlay> localPlays = LitePal.findAll(LocalPlay.class);//本地音乐播放信息
        for (LocalPlay localPlay : localPlays) {
            Song song = new Song();
            song.setSongId(localPlay.getSongId());
            song.setSingerName(localPlay.getAuthorName());
            song.setName(localPlay.getSongName());
            localList.add(song);
        }
    }
}